package com.example.controller;

import com.example.common.Result;
import com.example.domain.Article;
import com.example.service.ArticleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    ArticleService articleService;


    //查全部数据
    @GetMapping("/selectAll")
    public Result selectAll(Article article){
        List<Article> list = articleService.selectAll(article);
        return Result.success("查询成功",list);
    }

    //    @RequestBody              @PathVariable适用场景：根据ID查询资源
// 查单个数据
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Long id){
        Article article = articleService.selectByPrimaryKey(id);
        return Result.success("查询成功",article);
    }

    //分页查询数据   pageNum:当前页  pageSize：每页个数  -------  要先导入依赖
    @GetMapping("/selectPage")
    public Result selectPage( Article article,
                              @RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize){
        PageInfo<Article> pageInfo = articleService.selectPage(article,pageNum, pageSize);
        return Result.success("查询成功",pageInfo);
    }

    // 新增数据
    @PostMapping("/add")
    public Result add(@RequestBody Article article){
        articleService.add(article);
        return Result.success("新增成功",null);
    }

    //根据id  更新数据
    @PostMapping("/update")
    public Result update(@RequestBody Article article){
        articleService.update(article);
        return Result.success("更新成功",null);
    }

    //根据id  删除数据
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(Integer id){
        articleService.deleteById(id);
        return Result.success("删除成功",null);
    }

}
